package platform_api.Service;

import org.springframework.stereotype.Service;
import platform_api.Model.CommonInfo;
import platform_api.Model.JsonMessage;
import platform_api.Model.TradeData;
import platform_api.Repository.TradeDataRepository;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class TradeService extends BaseService {

    private final TradeDataRepository tradeDataRepository;

    public TradeService(TradeDataRepository tradeDataRepository) {
        this.tradeDataRepository = tradeDataRepository;
    }

    /**
     * 返回echart格式数据
     *
     * @param subject   商品代码
     * @param column    字段
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @return JsonMessage 数据
     */
    public JsonMessage getEchartData(String subject, String column, Date startTime, Date endTime, String distance) {
        if ("month".equals(distance)) {
            return getTradeDataOfEachMonth(subject, column, startTime, endTime);
        }
        return getTradeDataOfEachDay(subject, column, startTime, endTime);
    }

    /**
     * 按每日获取数据
     *
     * @param subject   商品代码
     * @param column    字段
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @return JsonMessage
     */
    private JsonMessage getTradeDataOfEachDay(String subject, String column, Date startTime, Date endTime) {
        List<TradeData> tradeDataList = tradeDataRepository.getByVarietyAndTransactionDate(startTime, endTime, subject);

        ArrayList<String> category = new ArrayList<>();
        ArrayList<String> data = new ArrayList<>();

        int i = 0;
        Date timeIterator = startTime;
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(startTime);
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");

        while (timeIterator.compareTo(endTime) < 0) {
            if (i < tradeDataList.size() && tradeDataList.get(i).getTransactionDate().equals(timeIterator)) {
                category.add(tradeDataList.get(i).getTransactionDate().toString());
                data.add(tradeDataList.get(i).getColumn(column));
                i += 1;
            } else {
                category.add(df.format(timeIterator));
                data.add("0");
            }
            calendar.add(Calendar.DATE, 1);
            timeIterator = calendar.getTime();
        }
        assert category.size() == data.size();

        return getJsonMessage(category, data, getMinValue(subject, column, startTime, endTime), getMaxValue(subject, column, startTime, endTime), TradeData.getColumnName(column));
    }

    /**
     * 按月获取数据
     *
     * @param subject   商品代码
     * @param column    字段
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @return JsonMessage
     */
    private JsonMessage getTradeDataOfEachMonth(String subject, String column, Date startTime, Date endTime) {

        ArrayList<String> category = new ArrayList<>();
        ArrayList<String> data = new ArrayList<>();

        Date timeIterator = startTime;
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(startTime);
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");

        while (timeIterator.compareTo(endTime) < 0) {
            Date tmp = timeIterator;
            calendar.add(Calendar.MONTH, 1);
            timeIterator = calendar.getTime();

            Object mean = tradeDataRepository.getMeanOfSingleColumn(subject, column, tmp, timeIterator);

            if (mean == null) {
                mean = 0;
            }

            if (mean instanceof Double) {
                mean = String.format("%.2f", mean);
            }

            category.add(df.format(tmp));
            data.add(mean.toString());
        }
        assert category.size() == data.size();

        return getJsonMessage(category, data, getMinValue(subject, column, startTime, endTime), getMaxValue(subject, column, startTime, endTime), TradeData.getColumnName(column));
    }

    /**
     * 获取字段含义
     *
     * @return JsonMessage
     */
    public JsonMessage getColumns() {
        Map<String, String> data = new HashMap<>();
        data.put("variety", "品种月份");
        data.put("settlement_yesterday", "昨日结算");
        data.put("opening_today", "今日开盘");
        data.put("highest_price", "最高价");
        data.put("lowest_price", "最低价");
        data.put("closing_today", "今日收盘");
        data.put("settlement_today", "今日结算");
        data.put("up_and_down1", "涨跌1");
        data.put("up_and_down2", "涨跌2");
        data.put("volume", "成交量");
        data.put("empty_quantity", "空盘量");
        data.put("increase_or_decrease", "增减量");
        data.put("turn_over", "成交额");
        data.put("delivery_settlement_price", "交割结算价");
        return new JsonMessage(200, data);
    }

    /**
     * 获取可选期货商品代码
     *
     * @return JsonMessage
     */
    public JsonMessage getVarieties() {
        List<String> str = tradeDataRepository.getVarieties();
        Map<String, List<String>> data = new HashMap<>();
        data.put("data", str);
        return new JsonMessage(200, data);
    }

    /**
     * 获取最小值和最大值
     *
     * @param variety   代码
     * @param column    字段
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @return Object
     */
    private Object getMinValue(String variety, String column, Date startTime, Date endTime) {
        Object value = tradeDataRepository.getMinValue(variety, column, startTime, endTime);
        if (value == null) {
            value = 0;
        }
        return value;
    }

    private Object getMaxValue(String variety, String column, Date startTime, Date endTime) {
        Object value = tradeDataRepository.getMaxValue(variety, column, startTime, endTime);
        if (value == null) {
            value = 0;
        }
        return value;
    }

    /**
     * 获取苹果期货代码对应产品
     *
     * @return JsonMessage
     */
    public JsonMessage getAppleColumns() {
        List<CommonInfo> str = new LinkedList<>();
        Map<String, List<CommonInfo>> data = new HashMap<>();
        str.add(new CommonInfo("AP805", "苹果805"));
        str.add(new CommonInfo("AP807", "苹果807"));
        str.add(new CommonInfo("AP810", "苹果810"));
        str.add(new CommonInfo("AP811", "苹果811"));
        str.add(new CommonInfo("AP812", "苹果812"));
        str.add(new CommonInfo("AP901", "苹果901"));
        str.add(new CommonInfo("AP903", "苹果903"));
        str.add(new CommonInfo("AP905", "苹果905"));
        str.add(new CommonInfo("AP907", "苹果907"));
        str.add(new CommonInfo("AP910", "苹果910"));
        data.put("data", str);
        return new JsonMessage(200, data);
    }
}
